<style>
</style>
<template>
    <section class="clearfix content">
        <h4 class="detail-hd">详情<button @click="toModify" v-if="type==1" class="fr btn-default">编辑</button></h4>
        <ul class="detail-list">
            <li><label for="">标题一</label>
                <span v-if="type == 1">{{param.title}}</span>
                <span v-if="type != 1"><input type="text" v-model="param['title']"></span>
            </li>
            <li><label for="">标题二</label>
                <span v-if="type == 1">{{param.content}}</span>
                <span v-if="type != 1"><input type="text" v-model="param['content']"></span>
            </li>
            <li><label for="">消息类型</label>
                <span v-if="type == 1">{{types[param.type]}}</span>
                <span v-if="type != 1">
                    <select name="" v-model="param['type']" id="">
                        <option v-for="(key,val) in types" value="{{key}}">{{val}}</option>
                    </select>
                </span>
            </li>
            <li><label for="">推送范围</label>
                <span v-if="type == 1">{{scope[param.scope]}}</span>
                <span v-if="type != 1">
                     <select name="" v-model="param['scope']" id="">
                         <option v-for="(key,val) in scope" value="{{key}}">{{val}}</option>
                     </select>
                </span>
            </li>
            <li><label for="">对象</label>
                <span v-if="type == 1"><span v-for="its in param.aliases">{{its+','}}</span></span>
                <span v-if="type != 1"><input type="text" v-model="param['aliases']"></span>
            </li>
            <li><label for="">简介</label>
                <span v-if="type == 1">{{param.url}}</span>
                    <span v-if="type != 1">
                        <textarea name=""style="height: 90px;width: 60%" id="" cols="30"  v-model="param['url']" rows="10"></textarea>
                    </span>
            </li>
        </ul>
        <div class="sub-group"  v-if="type==0">
            <button @click="modify">保存</button>
            <button @click="back">返回</button>
        </div>
    </section>
</template>
<script>
    module.exports = {
        data: function() {
            return {
                list:[],
                pageNo:1,
                pageSize:VIE.pageSize,
                totalPage:'',
                pages:[],
                param:{
                    type:'',
                    scope:'',
                    content:'',
                    title:'',
                    url:'',
                    aliases:[],
                },
                type:0,
                types:{
                    WEBSITE:'网页',
                    VIDEO:'视频',
                    TEXT:'文本',
                    GOODS:'商品',
                    PHYSICAL:'体检套餐',
                    DRUG:'药品',
                },
                scope:{
                    ALL:'所有用户',
                    PARTIAL:'部分用户',
                },
            }
        },
        methods: {
            render: function(){
                var params = {};
                VIE.getJ(HTTP_SERVER+'api/message/'+VIE.QueryString('id'),params,this,this.success,this.error);
            },
            success: function(data){
                this.param.content = data.content;
                this.param.scope = data.scope;
                this.param.type = data.type;
                this.param.aliases = data.aliases;
                this.param.url = data.url;
                this.param.title = data.title;
            },
            error: function(){

            },
            toggleAside: function(){
                $(".nav-aside>ul,.nav-aside>.asidedetail").toggleClass('all');
            },
            back: function(){
                router.go("message");
            },
            modify: function(){
                var arr = this.param.aliases;
                if(this.param.aliases instanceof Array == false){
                    this.param.aliases = arr.split(',');
                }
                if(VIE.QueryString('id')){
                    this.param.id = VIE.QueryString('id');
                    VIE.putJ(HTTP_SERVER+'api/message',this.param,this,this.toList,this.error);
                }else{
                    VIE.postJ(HTTP_SERVER+'api/message',this.param,this,this.toList,this.error);
                }
            },
            toModify: function(){
                this.type = 0;
            },
            toList: function(data){
                router.go("message");
            },
            upload: function(){
                VIE.upload("image",HTTP_SERVER+'api/upload/single',this,this.geturl,this.error);
            },
            uploads: function(){
                VIE.upload("photos",HTTP_SERVER+'api/upload/single',this,this.geturls,this.error);
            },
            deleteImg: function(ind){
                var _this = this;
                var data = _this.param.photos;
                data.splice(ind,1);
                _this.param.photos = data;
            },
            geturls: function(data){
                var _this = this;
                var datas = [];
                datas = _this.param.photos;
                datas.push(data.url);
                _this.param.photos = datas;
            },
            geturl: function(data){
                var _this = this;
                _this.param.image = data.url;
            },
        },
        ready: function() {
            var _this = this;
            var loads = function(){
                if(VIE.QueryString('type')){
                    _this.type = 1;

                    _this.render();
                }else{
                    _this.type = 0;
                }
                _this.param = {
                    type:'',
                    scope:'',
                    content:'',
                    url:'',
                    title:'',
                    aliases:[],
                };
            }
            var name = window.location.hash.split('?')[0].replace("#/","");
            if(!name){
                name = "user";
            }
            if(!router.fn[name]){
                loads();
                router.fn[name] = loads;
            }

        }
    };

</script>